package cn.sxt.common.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import cn.sxt.common.dao.IBaseDao;
import cn.sxt.common.util.ConstatFinalUtil;
import cn.sxt.common.util.PageInfoUtil;

/**
 * dao实现类的父类
 * @author wangshSxt
 *
 */
public abstract class BaseDaoImpl<T> extends HibernateDaoSupport implements IBaseDao<T>
{
	@Override
	public int save(T t)
	{
		return (int) this.getHibernateTemplate().save(t);
	}

	@Override
	public int update(T t)
	{
		try
		{
			this.getHibernateTemplate().update(t);
			/*String str = null ; 
			str.toString() ; */
			return 1 ; 
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("更新对象失败了;", e);
			throw e ; 
		}
	}

	public abstract T findOneDao(Map<String, Object> condMap);

	public abstract List<T> findCondListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap);
	
	/**
	 * 查询单条记录
	 * @return
	 */
	protected T findSinge(String hql,Map<String, Object> paramsMap)
	{
		/* 调用Hibernate查询一条记录 */
		return this.getHibernateTemplate().execute(new HibernateCallback<T>()
		{
			@Override
			public T doInHibernate(Session session) throws HibernateException
			{
				/* 查询一条记录 */
				Query query = session.createQuery(hql);
				/* 设置查询条件 */
				query.setProperties(paramsMap);
				return (T) query.uniqueResult();
			}
		});
	}
	
	/**
	 * 分页查询
	 * @return
	 */
	protected List<T> findByPage(PageInfoUtil pageInfoUtil,String hql,Map<String, Object> paramsMap)
	{
		/* 调用Hibernate查询一条记录 */
		return this.getHibernateTemplate().execute(new HibernateCallback<List<T>>()
		{
			@Override
			public List<T> doInHibernate(Session session) throws HibernateException
			{
				/* 先查询总记录数
				 * hql:from AAdmins where 1 = 1 and status = 1 order by createTime desc
				 * 查询总记录数:select count(*) from AAdmins where 1 = 1 and status = 1 
				 * 查询分页数据:from AAdmins where 1 = 1 and status = 1 order by createTime desc limit ? , ? 
				 *  */
				String countSql = "select count(*) " ;
				countSql += hql.substring(hql.indexOf("from"), hql.indexOf("order by"));
				ConstatFinalUtil.LOGGER.info("查询总记录数的SQL:{}",countSql);
				Query query = session.createQuery(countSql);
				/* 设置查询条件 */
				query.setProperties(paramsMap);
				/* 总记录数的返回值待定 */
				Long totalRecord = (Long) query.uniqueResult() ; 
				pageInfoUtil.setTotalRecord(totalRecord.intValue());
				
				/* 查询分页数据 */
				query = session.createQuery(hql.toString());
				query.setProperties(paramsMap);
				/* 分页
				 * limit ? , ? 
				 *  */
				/* 第一个? */
				query.setFirstResult(pageInfoUtil.getCurrentRecord());
				/* 第二个? */
				query.setMaxResults(pageInfoUtil.getPageSize());
				return query.list();
			}
		});
	}
	
	/**
	 * 不分页查询
	 * @param hql
	 * @param paramsMap
	 * @return
	 */
	protected List<T> find(String hql,Map<String, Object> paramsMap)
	{
		return this.getHibernateTemplate().execute(new HibernateCallback<List<T>>()
		{
			@Override
			public List<T> doInHibernate(Session session) throws HibernateException
			{
				/* 查询一条记录 */
				Query query = session.createQuery(hql.toString());
				/* 设置查询条件 */
				query.setProperties(paramsMap);
				return query.list();
			}
		});
	}
}
